package Action;

import java.util.ArrayList;
import java.util.List;

import Beans.Agent;
import Beans.User;
import DAO.AgentDao;
import DAO.UserDao;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UserAction extends ActionSupport {

	private List<User> users;
	private List<Agent> agents;

	UserDao userDao = new UserDao();
	AgentDao agentDao = new AgentDao();

	private int userID;
	private int agentID;
	private String phoneNum;
	private User user = new User();

	/**
	 * 编辑用户
	 * 
	 * @return
	 */
	public String editUser() {
		User u = userDao.getUserById(userID);
		user.setId(userID);
		user.setAgent(agentDao.getAgentById(agentID));
		user.setStatus(u.getStatus());
		user.setWxID(u.getWxID());
		userDao.edit(user);
		return "success";
	}

	/**
	 * 删除用户
	 * 
	 * @return
	 */
	public String deleteUser() {
		user = userDao.getUserById(userID);
		user.setStatus("0");
		userDao.edit(user);
		return "success";
	}
	/**
	 * 解绑rfid
	 * 
	 * @return
	 */
	public String deleteRfid() {
		user = userDao.getUserById(userID);
		user.setRfid(null);
		userDao.edit(user);
		return "success";
	}
	/**
	 * 解绑微信
	 * 
	 * @return
	 */
	public String deleteWxID() {
		user = userDao.getUserById(userID);
		user.setWxID(null);
		userDao.edit(user);
		return "success";
	}

	/**
	 * 新增用户
	 * 
	 * @return
	 */
	public String registerUser() {
		user.setAgent(agentDao.getAgentById(agentID));
		user.setStatus("2");
		user.setRfid(user.getRfid().substring(2).toUpperCase());
		users = userDao.getAllUserPhoneNum(user.getMobileNum());
		if (users.size() == 0) {
			userDao.save(user);
		} else {
			user.setId(users.get(0).getId());
			userDao.edit(user);
		}
		return "success";
	}

	/**
	 * 通过ID获取用户
	 */
	public String getUserById() {
		agents = agentDao.getAllAgent();
		user = userDao.getUserById(userID);
		return "success";
	}
	
	
	public String getByUserID() {
		users = new ArrayList<User>();
		user = userDao.getUserById(userID);
		users.add(user);
		return "success";
	}

	/**
	 * 获取所有用户
	 */
	public String getAllUser() {
		users = userDao.getAllUser();
		return "success";
	}

	/**
	 * 获取所有代理商
	 */
	public String addUser() {
		agents = agentDao.getAllAgent();
		return "success";
	}

	/**
	 * 通过手机号码筛选用户
	 */
	public String getAllUserPhoneNum() {
		users = userDao.getAllUserPhoneNum(phoneNum);
		return "success";
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public int getUserID() {
		return userID;
	}

	public void setUserID(int userID) {
		this.userID = userID;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getPhoneNum() {
		return phoneNum;
	}

	public void setPhoneNum(String phoneNum) {
		this.phoneNum = phoneNum;
	}

	public List<Agent> getAgents() {
		return agents;
	}

	public void setAgents(List<Agent> agents) {
		this.agents = agents;
	}

	public int getAgentID() {
		return agentID;
	}

	public void setAgentID(int agentID) {
		this.agentID = agentID;
	}

}
